﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Web;
using System.Web.Caching;
using StripeOne.Core.S1Tools;

namespace StripeOne.Core.S1Cache
{
    public static class S1IISCache
    {
        public static void Add(object obj, string key, int seconds)
        {
            if (HttpContext.Current != null)
            {
                S1CacheItem cacheItem = new S1CacheItem();
                cacheItem.Expiration = DateTime.Now.AddSeconds((double)seconds);
                cacheItem.Data = obj;
                cacheItem.Type = S1CacheItemType.IISCache;
                Cache cache = HttpContext.Current.Cache;
                if (cache != null && key != "" && seconds > 0)
                {
                    cache.Insert(key, cacheItem, null, DateTime.Now.AddSeconds((double)seconds), TimeSpan.Zero);
                }
            }
        }
        public static void Remove(string key)
        {
            if (HttpContext.Current != null)
            {
                Cache cache = HttpContext.Current.Cache;
                if (cache != null && key != "")
                {
                    cache.Remove(key);
                }
            }
        }
        public static object GetObject(string key, out int remaining_seconds)
        {
            try
            {
                if (HttpContext.Current != null)
                {
                    Cache cache = HttpContext.Current.Cache;
                    object obj = cache.Get(key);
                    if (cache != null && obj is S1CacheItem && ((S1CacheItem)obj).Type == S1CacheItemType.IISCache)
                    {
                        S1CacheItem CacheItem = (S1CacheItem)obj;
                        remaining_seconds = (int)Math.Ceiling((CacheItem.Expiration - DateTime.Now).TotalSeconds);
                        return CacheItem.Data;
                    }
                }
            }
            catch (Exception ex)
            {
                string text = "Error reading cache key '" + key + "' from IISCache: " + ex.Message;
                if (ex.InnerException != null)
                {
                    text = text + Environment.NewLine + "Inner Exception: " + ex.InnerException.Message;
                }
            }
            remaining_seconds = 0;
            return null;
        }
        public static object GetObject(string key)
        {
            int num;
            return S1IISCache.GetObject(key, out num);
        }
        public static string GetString(string key)
        {
            return S1IISCache.GetObject(key).ToString();
        }
        public static string GetString(string key, string defaultValue)
        {
            return S1IISCache.GetObject(key).ToString(defaultValue);
        }
        public static byte GetByte(string key)
        {
            return S1IISCache.GetObject(key).ToByte();
        }
        public static byte GetByte(string key, byte defaultValue)
        {
            return S1IISCache.GetObject(key).ToByte(defaultValue);
        }
        public static short GetInt16(string key)
        {
            return S1IISCache.GetObject(key).ToInt16();
        }
        public static short GetInt16(string key, short defaultValue)
        {
            return S1IISCache.GetObject(key).ToInt16(defaultValue);
        }
        public static int GetInt32(string key)
        {
            return S1IISCache.GetObject(key).ToInt32();
        }
        public static int GetInt32(string key, int defaultValue)
        {
            return S1IISCache.GetObject(key).ToInt32(defaultValue);
        }
        public static long GetInt64(string key)
        {
            return S1IISCache.GetObject(key).ToInt64();
        }
        public static long GetInt64(string key, long defaultValue)
        {
            return S1IISCache.GetObject(key).ToInt64(defaultValue);
        }
        public static decimal GetDecimal(string key)
        {
            return S1IISCache.GetObject(key).ToDecimal();
        }
        public static decimal GetDecimal(string key, decimal defaultValue)
        {
            return S1IISCache.GetObject(key).ToDecimal(defaultValue);
        }
        public static decimal GetDecimal(string key, string cultureName)
        {
            return S1IISCache.GetObject(key).ToDecimal(cultureName);
        }
        public static decimal GetDecimal(string key, string cultureName, decimal defaultValue)
        {
            return S1IISCache.GetObject(key).ToDecimal(cultureName, defaultValue);
        }
        public static decimal GetDecimal(string key, CultureInfo cultureInfo)
        {
            return S1IISCache.GetObject(key).ToDecimal(cultureInfo);
        }
        public static decimal GetDecimal(string key, CultureInfo cultureInfo, decimal defaultValue)
        {
            return S1IISCache.GetObject(key).ToDecimal(cultureInfo, defaultValue);
        }
        public static float GetSingle(string key)
        {
            return S1IISCache.GetObject(key).ToSingle();
        }
        public static float GetSingle(string key, float defaultValue)
        {
            return S1IISCache.GetObject(key).ToSingle(defaultValue);
        }
        public static float GetSingle(string key, CultureInfo cultureInfo)
        {
            return S1IISCache.GetObject(key).ToSingle(cultureInfo);
        }
        public static float GetSingle(string key, CultureInfo cultureInfo, float defaultValue)
        {
            return S1IISCache.GetObject(key).ToSingle(cultureInfo, defaultValue);
        }
        public static float GetSingle(string key, string cultureName)
        {
            return S1IISCache.GetObject(key).ToSingle(cultureName);
        }
        public static float GetSingle(string key, string cultureName, float defaultValue)
        {
            return S1IISCache.GetObject(key).ToSingle(cultureName, defaultValue);
        }
        public static float GetFloat(string key)
        {
            return S1IISCache.GetObject(key).ToFloat();
        }
        public static float GetFloat(string key, float defaultValue)
        {
            return S1IISCache.GetObject(key).ToFloat(defaultValue);
        }
        public static float GetFloat(string key, CultureInfo cultureInfo)
        {
            return S1IISCache.GetObject(key).ToFloat(cultureInfo);
        }
        public static float GetFloat(string key, CultureInfo cultureInfo, float defaultValue)
        {
            return S1IISCache.GetObject(key).ToFloat(cultureInfo, defaultValue);
        }
        public static float GetFloat(string key, string cultureName)
        {
            return S1IISCache.GetObject(key).ToFloat(cultureName);
        }
        public static float GetFloat(string key, string cultureName, float defaultValue)
        {
            return S1IISCache.GetObject(key).ToFloat(cultureName, defaultValue);
        }
        public static double GetDouble(string key)
        {
            return S1IISCache.GetObject(key).ToDouble();
        }
        public static double GetDouble(string key, double defaultValue)
        {
            return S1IISCache.GetObject(key).ToDouble(defaultValue);
        }
        public static double GetDouble(string key, string cultureName)
        {
            return S1IISCache.GetObject(key).ToDouble(cultureName);
        }
        public static double GetDouble(string key, string cultureName, double defaultValue)
        {
            return S1IISCache.GetObject(key).ToDouble(cultureName, defaultValue);
        }
        public static double GetDouble(string key, CultureInfo cultureInfo)
        {
            return S1IISCache.GetObject(key).ToDouble(cultureInfo);
        }
        public static double GetDouble(string key, CultureInfo cultureInfo, double defaultValue)
        {
            return S1IISCache.GetObject(key).ToDouble(cultureInfo, defaultValue);
        }
        public static bool GetBoolean(string key)
        {
            return S1IISCache.GetObject(key).ToBoolean();
        }
        public static bool GetBoolean(string key, bool defaultValue)
        {
            return S1IISCache.GetObject(key).ToBoolean(defaultValue);
        }
        public static DateTime GetDateTime(string key)
        {
            return S1IISCache.GetObject(key).ToDateTime();
        }
        public static DateTime GetDateTime(string key, DateTime defaultValue)
        {
            return S1IISCache.GetObject(key).ToDateTime(defaultValue);
        }
        public static DateTime GetDateTime(string key, string cultureName)
        {
            return S1IISCache.GetObject(key).ToDateTime(cultureName);
        }
        public static DateTime GetDateTime(string key, string cultureName, DateTime defaultValue)
        {
            return S1IISCache.GetObject(key).ToDateTime(cultureName, defaultValue);
        }
        public static DateTime GetDateTime(string key, CultureInfo cultureInfo)
        {
            return S1IISCache.GetObject(key).ToDateTime(cultureInfo);
        }
        public static DateTime GetDateTime(string key, CultureInfo cultureInfo, DateTime defaultValue)
        {
            return S1IISCache.GetObject(key).ToDateTime(cultureInfo, defaultValue);
        }
        public static TimeSpan GetTimeSpan(string key)
        {
            return S1IISCache.GetObject(key).ToTimeSpan();
        }
        public static TimeSpan GetTimeSpan(string key, TimeSpan defaultValue)
        {
            return S1IISCache.GetObject(key).ToTimeSpan(defaultValue);
        }
    }
}
